package main;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BigFileFilter {

	public static final String INPUT_FILE = "D:\\ajpcell.exch";
	public static final String OUTPUT_FILE = "D:\\ajpcell.exch.txt";
	public static final String OUTPUT_LAST_NUM_FILE = "D:\\ajpcell.exch.last.txt";
	public static final int M_SIZE = 1048576;
	public static final int BUFFER_SIZE_M = 5;
	public static final int BUFFER_SIZE = M_SIZE * BUFFER_SIZE_M;

	public BigFileFilter() {
		String str1E = String.valueOf((char) 30);
		String str1D = String.valueOf((char) 29);
		try {
			Pattern p = Pattern.compile(str1E + str1D + "(\\d+\\.\\d+\\.\\d+\\.\\d+\\.\\d+)" + str1E);
			Pattern p1 = Pattern.compile("(\\d+\\.\\d+\\.\\d+\\.\\d+\\.\\d+)" + str1E);
			char[] c = new char[BUFFER_SIZE];
			StringBuffer sb = new StringBuffer();
			FileInputStream fis = new FileInputStream(INPUT_FILE);
			InputStreamReader isr = new InputStreamReader(fis);
			FileOutputStream fos = new FileOutputStream(OUTPUT_FILE);
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			FileOutputStream fosl = new FileOutputStream(OUTPUT_LAST_NUM_FILE);
			OutputStreamWriter oswl = new OutputStreamWriter(fosl);
			int i = 0;
			while (isr.read(c) != -1) {
				// if (result == -1) {
				// System.out.println();
				// }
				i++;

				String tmp = new String(c);
				sb.append(tmp);
				if (i == 1) {
					Matcher m1 = p1.matcher(sb);
					if (m1.find()) {
						write(osw, m1.group(1));
						writeLastNum(oswl, m1.group(1));
					}
				}
				String totalstr = sb.toString();
				Matcher m = p.matcher(sb);
				while (m.find()) {

					write(osw, m.group(1));
					writeLastNum(oswl, m.group(1));

					if (m.hitEnd()) {
						sb = new StringBuffer(totalstr.substring(m.end()));
					} else {
						sb = new StringBuffer();
					}
				}
				osw.flush();
				c = new char[BUFFER_SIZE];
				System.out.println(i * BUFFER_SIZE_M + "M processed...");
			}
			osw.close();
			oswl.close();
			isr.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private void write(OutputStreamWriter osw, String output) throws IOException {
		osw.write(output);
		osw.write("\n");
	}

	private void writeLastNum(OutputStreamWriter osw, String output) throws IOException {
		String[] tmp = output.split("\\.");
		osw.write(tmp[4] + "\n");
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Date begin = new Date();
		BigFileFilter a = new BigFileFilter();
		Date end = new Date();
		System.out.println("total time:" + (end.getTime() - begin.getTime()));
	}

}
